package gamegame;

import java.util.Scanner;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class GameGame {
	private static Object lock = new Object();
	private static JFrame passFrame;
	public static void makeWindow(JFrame w, int minx, int miny)
	{
		w.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		w.setSize(minx,miny);
		w.setMinimumSize(new Dimension(minx,miny));
		w.setLocationRelativeTo(null);	
		w.setResizable(true);
		w.setVisible(true);
		w.setAlwaysOnTop(true);
		w.setAlwaysOnTop(false);
	}
	public static void initialize(MapLocation[][] map)
	{
		/**
		 * Sup Lucas! Here's how the basic engine I scrapped together goes:
		 * A.) We initialize a location in our map to have a basic description as well as a maximum number of actions.
		 * B.) We create an array of possible commands that correspond to the same action ("look wall" and "examine wall" are identical, for instance).
		 * C.) We use addCommand(array, anonymous class) to make a command for that map location.
		 * D.) action(String anything) takes in the command and returns an int. We can make it better later. It's 3 A.M.
		 * E.) Repeat B-D for all commands in a given map location.
		 * F.) Move on to the next map location.
		 * G.) The int returned can be used to dictate whether or not the default location description is redisplayed, as well as the change and direction.
		 */
		System.out.println("CLANG. \n\n" +
		"A lone footstep tears you from sleep.  You try to stand but stumble, quickly falling once more to your knees. \n\n " +
		"CLANG.\n\n" +
		"You begin to regain your faculties as you look around.  Four walls surround you, with a low-hanging ceiling above. \n\n" +
		"CLANG.\n\n" +
		"The steps approach the door to your cell.  You notice you are bleeding.  Red fluid stains your clothes and drips beneath you as you begin to panic.  You notice a vent above.\n" +
		"With all of your might you tear the vent from the ceiling, sending it crashing to the ground below.  \n" +
		"The footsteps stop.  You hear the handle rattle on the door as someone tries to enter.  You leap up into the vent, desperately trying to hoist yourself into the shaft.\n\n" +
		"CLANG.\n\n" +
		"You scurry away through the vent as the door flies open.  Upon reaching another vent cover, you unscrew it and drop down.");
		
		map[0][0] = new MapLocation("You look around.  Three walls rise to the east, west, and south.  An open vent lies above.",4);
		String[] commands = new String[2];
		commands[0] = "get into vent";
		commands[1] = "use vent";
		map[0][0].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("Better not go back that way.");
				return 0;
			}
		});
		commands = new String[10];
		commands[0] = "go east";
		commands[1] = "go west";
		commands[2] = "go south";
		commands[3] = "east";
		commands[4] = "south";
		commands[5] = "west";
		commands[6] = "examine wall";
		commands[7] = "look wall";
		commands[8] = "examine walls";
		commands[9] = "look walls";
		map[0][0].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("These walls are boring as fuck.");
				return 0;
			}
		});
		commands = new String[5];
		commands[0] = "up";
		commands[1] = "look up";
		commands[2] = "look sky";
		commands[3] = "examine up";
		commands[4] = "examine sky";
		map[0][0].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("You peek your head into the vent. \nOccasionally, if you strain your ears really hard, you can" +
						" hear the sound of somebody going \"hhhhhhhhggg\" as if they were having bowel problems.");
				return 0;
			}
		});
		commands = new String[2];
		commands[0] = "north";
		commands[1] = "go north";
		map[0][0].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println();
				return 11;
			}
		});
		map[0][1] = new MapLocation("You see a control panel in front of you. You can return south, if you are a pussy.", 5);
		commands = new String[6];
		commands[0] = "go east";
		commands[1] = "go west";
		commands[2] = "go north";
		commands[3] = "east";
		commands[4] = "west";
		commands[5] = "north";
		map[0][1].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("You can't go that way. Really. Stop trying.");
				return 0;
			}
		});
		commands = new String[7];
		commands[0] = "up";
		commands[1] = "look up";
		commands[2] = "look sky";
		commands[3] = "examine up";
		commands[4] = "examine sky";
		commands[5] = "examine surroundings";
		commands[6] = "look around";
		map[0][1].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("Now even the walls have disappeared. Your surroundings remain an eerie black.\n" +
						"Somewhere in the background you hear an advertisement for the Hook, Line, and Q-Tip.");//DUDE
				return 0;
			}
		});
		commands = new String[6];
		commands[0] = "examine panel";
		commands[1] = "examine control panel";
		commands[2] = "look panel";
		commands[3] = "look control panel";
		commands[4] = "examine north";
		commands[5] = "look north";
		map[0][1].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("The control panel to your north appears to be a passcode-entering mechanism.\n"+
						"Luckily, the code is written right on the panel next to the keys.");
				return 0;
			}
		});
		commands = new String[4];
		commands[0] = "use panel";
		commands[1] = "use control panel";
		commands[2] = "activate panel";
		commands[3] = "activate control panel";
		map[0][1].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("You see a passcode thing and look at it.");
				
				passFrame = new PassCodeEnter("6969");
				passFrame.addWindowListener(new WindowAdapter() {
				    public void windowClosing(WindowEvent arg0) {
				        synchronized (lock) {
				            passFrame.setVisible(false);
				            lock.notify();
				        }
				    }
				});
				synchronized(lock){
					makeWindow(passFrame,210,220);
					while(passFrame.isVisible()){
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					if(passFrame.toString().equals("6969X"))
						return 2;
					System.out.println("WRONG PASSCODE INSERTED. GO LICK A STICK OF DYNAMITE, TURD EARS.");
					return 1;
				}
			}
		});
		commands = new String[2];
		commands[0] = "south";
		commands[1] = "go south";
		map[0][1].addCommand(commands, new MapLocation.Command(){
			public int action(String anything){
				System.out.println("You retreat south...pussy.");
				return 101;
			}
		});
	}
	public static void main(String[] args)
	{
		int change = 1; //Both indicates change in map location and whether or not description is reiterated.
		String cmd;
		Scanner sc = new Scanner(System.in);
		int[] yourLocation = new int[2];
		MapLocation[][] map = new MapLocation[1][2]; //Size of map. It's small for now, but grow it shall!
		initialize(map); //Separated into a separate method for organizational clarity.
		yourLocation[0] = 0;
		yourLocation[1] = 0;
		while(change != 2)
		{
			if(change != 0)
				System.out.println(map[yourLocation[0]][yourLocation[1]]); //Prints the map area description.
			System.out.print(">");
			cmd = sc.nextLine();
			change = map[yourLocation[0]][yourLocation[1]].execute(cmd); //Executes command.
			/**
			 * left right down up display (10101 means go left one down one display new location description)
			 */
			yourLocation[0] -= change / 10000;
			change %= 10000;
			yourLocation[0] += change / 1000;
			change %= 1000;
			yourLocation[1] -= change / 100;
			change %= 100;
			yourLocation[1] += change / 10;
			change %= 10;
		}
		System.out.println("You end the game and realize that you are Lucas Schairer/Raymond Sun. BAD END");
	}
}
